//
//  MainTabViewController.m
//  TicketSystem
//
//  Created by ZYVincent on 12-2-19.
//  Copyright (c) 2012年 __ZYProSoft__. All rights reserved.
//  http://www.ruyijian.com
//    团队QQ群号：219357847
//    团队主题：奋斗路上携手相伴！

#import "MainTabViewController.h"
#import "HomeViewController.h"
#import "RestaurantViewController.h"
#import "HouseViewController.h"
#import "MemberViewController.h"
#import "ConfigViewController.h"
#import "LoginViewController.h"
#import "OrderListViewController.h"
#define RED_VALUE 2
#define GREEN_VALUE 75
#define BLUE_VALUE 104
#define COLOR_ALPHA 1

@implementation MainTabViewController
@synthesize customTabBarItems;

//替换默认得tabbar
- (void)initCustomTabBar
{
    images = [NSArray arrayWithObjects:@"ic_home_bottombar", @"ic_search_bottombar",@"ic_order_bottombar",@"ic_myjlh_bottombar",@"ic_more_bottombar1",nil];
    selectedImages = [NSArray arrayWithObjects:@"ic_home_bottombar_selected", @"ic_search_bottombar_selected",@"ic_order_bottombar_selected",@"ic_myjlh_bottombar_selected",@"ic_more_bottombar_selected1",nil];

    customTabBarItems=[[NSMutableArray alloc]init];
    
    //自定义tabbar
    //if(IS_SDK_7)
    //{
           // self.tabBar.frame=CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y-20, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
    //}
    UIView *customTabBar=[[UIView alloc]init];
    customTabBar.frame=self.tabBar.frame;
    customTabBar.frame=self.tabBar.bounds;
    [self.tabBar addSubview:customTabBar];

    for (int i=0;i<5; i++) {
        
        NSString *tabBarClickedImageName=[selectedImages objectAtIndex:i];//[NSString stringWithFormat:@"bar%dSelected.png",i];
        NSString *tabBarImageName=[images objectAtIndex:i];//NSString stringWithFormat:@"bar%dNormal.png",i];
        
        UICustomTabBar *tabBar=[[UICustomTabBar alloc]
                                initWithNormalStateImage:[UIImage imageNamed:tabBarImageName] 
                                andClickedStateImage:[UIImage imageNamed:tabBarClickedImageName]];
        
        tabBar.frame = CGRectMake(64*i,1,64,64);
        tabBar.delegate=self;
        tabBar.tabBarTag=i;

        [customTabBar addSubview:tabBar];
        [customTabBarItems addObject:tabBar];
        
        //如果是第一个那么默认是选中的
        if (0==i) {
            [tabBar switchToClickedState];
        }
    }
}
//tabbar代理方法
- (void)tabBarDidTapped:(id)sender{
    
    UICustomTabBar *tabBar=(UICustomTabBar*)sender;
    for (UICustomTabBar *tabBarInstance in customTabBarItems) {
        if(tabBarInstance.tabBarTag==3 && tabBarInstance.tag==1)
        {
            continue;
        }
        [tabBarInstance switchToNormalState];
    }
    [tabBar switchToClickedState];
    //NSLog(@"%d",tabBar.tabBarTag);
    UIViewController *selectedController=[self.viewControllers objectAtIndex:tabBar.tabBarTag];
    //从预订页面没有登陆，登陆后再返回首页，再点击memberview，此时是loginview。需要手工pop回去。
    if (tabBar.tabBarTag==3) {
        UINavigationController *navigationctr = (UINavigationController *)selectedController;
        if ([navigationctr.topViewController isKindOfClass:[LoginViewController class]]) {
            [navigationctr.topViewController.navigationController popViewControllerAnimated:NO];
        }
        tabBar.tag=0;
    }
    self.selectedViewController=selectedController;
    
}
- (void) selectViewController:(int) index
{
    UICustomTabBar *tabBar= [customTabBarItems objectAtIndex:index];
    for (UICustomTabBar *tabBarInstance in customTabBarItems) {
        [tabBarInstance switchToNormalState];
    }
    [tabBar switchToClickedState];
    //NSLog(@"%d",tabBar.tabBarTag);
    UIViewController *selectedController=[self.viewControllers objectAtIndex:tabBar.tabBarTag];
    self.selectedViewController=selectedController;
    
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //设置tabbar颜色
        //self.tabBar.tintColor = [UIColor colorWithRed:RED_VALUE/255.0 green:GREEN_VALUE/255.0 blue:BLUE_VALUE/255.0 alpha:COLOR_ALPHA];
        //[self.tabBar setBackgroundImage:[UIImage imageNamed:@"banner.png"]];
        
        [[UITabBarItem appearance] setTitleTextAttributes:@{ UITextAttributeTextColor : [UIColor whiteColor],UITextAttributeFont : [UIFont fontWithName:@"ArialMT"size:10]}
                                                 forState:UIControlStateNormal];
        [[UITabBarItem appearance] setTitleTextAttributes:@{ UITextAttributeTextColor : TEXTFIELD_HOTCOLOR ,UITextAttributeFont : [UIFont fontWithName:@"Arial-BoldMT"size:10]}
                                                 forState:UIControlStateSelected];

        HomeViewController *detailController1 = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
        [detailController1.tabBarItem
         setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                 [UIColor orangeColor], UITextAttributeTextColor,
                                 nil] forState:UIControlStateNormal];
        
        UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:detailController1];
        
        nav1.navigationBar.hidden = TRUE;
        RestaurantViewController *detailController2 = [[RestaurantViewController alloc] initWithNibName:@"RestaurantViewController" bundle:nil];
        UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:detailController2];
        nav2.navigationBar.hidden=TRUE;
        OrderListViewController *detailController3 = [[OrderListViewController alloc] initWithNibName:@"OrderListViewController" bundle:nil];
        UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:detailController3];
        nav3.navigationBar.hidden=TRUE;
        MemberViewController *detailController4 = [[MemberViewController alloc] initWithNibName:@"MemberViewController" bundle:nil];
        UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:detailController4];
        nav4.navigationBar.hidden=TRUE;

        ConfigViewController *detailController5 = [[ConfigViewController alloc] initWithNibName:@"ConfigViewController" bundle:nil];
        UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:detailController5];
        nav5.navigationBar.hidden=TRUE;
        //添加到tabbarcontroller
        NSMutableArray *navControllers = [NSMutableArray arrayWithObjects:nav1,nav2,nav3,nav4,nav5,nil];
        self.viewControllers = navControllers;
        
        [self initCustomTabBar];//自定tabbar
        
    }
    return self;
}

//立即刷新票类信息
/*- (void)refreshTicketDataNow
{
    if (self.selectedIndex == 1) {
        TicketPreordainViewController *viewController = [ticketQueryNavController.viewControllers objectAtIndex:0];
        [viewController refreshNow];
    }
}*/

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
